package gui;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ComboBoxModel;
import javax.swing.JList;

import metagrammar.Operator;
import util.OperatorWrapper;

public class OperatorList extends JList<OperatorWrapper> {
    
    private class ModelUpdater implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            updateModel();
        }
    }
    
    private final OperatorModelProvider modelProvider;
    
    public OperatorList(OperatorModelProvider modelProvider)
    {
        this.modelProvider = modelProvider;
        setPrototypeCellValue(new OperatorWrapper(Operator.undefinedOperator));
        updateModel();
    }
    
    /**
     * @param libraryComboBox TODO
     * 
     */
    public OperatorList(LibraryComboBox libraryComboBox)
    {
        this((OperatorModelProvider) libraryComboBox);
        libraryComboBox.addActionListener(new ModelUpdater());
    }
    
    /**
     * 
     */
    private void updateModel()
    {
        ComboBoxModel<OperatorWrapper> model = modelProvider.getOperatorModel();
        setModel(model);
        if(model.getSize() > 0) {
            setSelectedIndex(0);
            setPrototypeCellValue(null);
        }
        else {
            setPrototypeCellValue(new OperatorWrapper(Operator.undefinedOperator));
        }
    }
}